package com.hospital.search.module.schedule;

import com.hospital.search.module.service.TIndexRecordService;
import com.hospital.search.module.utils.PostIndexManager;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ClearOldIndex {
    @Autowired
    private TIndexRecordService tIndexRecordService;
    @Autowired
    private PostIndexManager postIndexManager;

    /**
     * 清理过期索引,保留最近一个不活跃索引
     */
    @XxlJob("clearOldIndex")
    public void clearOldIndex(){
        List<String> oldIndexes = tIndexRecordService.findNotActive();
        XxlJobHelper.log("需要清理的索引: " + oldIndexes);
        for (String oldIndex : oldIndexes) {
            postIndexManager.deleteIndex(oldIndex);
        }
        if(tIndexRecordService.deleteNotActive()==0){
            XxlJobHelper.log("没有需要清理的索引");
        };
        XxlJobHelper.log("清理完成");
    }
}
